<template>
	<view class="page">
		<!-- <view class="flex benben-position-layout flex flex-wrap align-center purchaseData_flex_0"
			:style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx'}">
			<view class='flex flex-wrap align-center justify-between flex-sub purchaseData_fd0_0'>
				<view class='flex flex-wrap align-center purchaseData_fd0_0_c0' @tap.stop="handleJumpDiy"
					data-type="back" data-url="1">
					<image class='purchaseData_fd0_0_c0_c0' mode="aspectFit" src='/static/shop/back.png'></image>
				</view>
				<view class='flex flex-wrap align-center'>
					<text class='purchaseData_fd0_0_c1_c0'>预览</text>
				</view>
				<view class='flex flex-wrap align-center purchaseData_fd0_0_c2'>
				</view>
			</view>

		</view>
		<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view> -->

		<view style="width: 100%;">
			<web-view :src="pdfUrl"></web-view>
			<!-- <iframe ref="iframe" :src="pdfUrl" width="100%"
				height="100%" frameborder="0">
			</iframe> -->
		</view>


	</view>
</template>
<script>
	export default {
		components: {},

		data() {
			return {
				StatusBarRpx: 0,
				pdfUrl: '',
				htmlUrl: '/hybrid/html/web/viewer.html',
				fileUrl: {},
				id: '', // 章节id
				course_id: "", // 课程id
				name: "", // 章节名称
				benMusic: "",
				is_show: false,
				recordObj: "",
				watchTime: 0,
			};
		},
		computed: {

		},
		watch: {},
		onLoad(options) {
			let {
				id,
				name,
				course_id,
				current_duration
			} = options;
			if (id !== undefined) this.id = id;
			if (name !== undefined) this.name = name;
			if (course_id !== undefined) this.course_id = course_id;
			this.fileUrl = uni.getStorageSync('fileUrl');
			this.pdfUrl = this.htmlUrl + '?file=' + this.fileUrl.path;
			// this.pdfUrl = 'www.baidu.com';
			uni.getSystemInfo({
				success: (e) => {
					// this.compareVersion(e.SDKVersion, '2.5.0')
					let statusBar = 0 //状态栏高度
					let customBar = 0 // 状态栏高度 + 导航栏高度  
					let navbar = 0 // 自定义标题与胶囊对齐高度
					// #ifdef MP
					statusBar = e.statusBarHeight
					customBar = e.statusBarHeight + 45
					if (e.platform === 'android') {
						this.$store.commit('SET_SYSTEM_IOSANDROID', false)
						customBar = e.statusBarHeight + 50
					}
					// #endif
					// #ifdef MP-WEIXIN
					statusBar = e.statusBarHeight
					const custom = wx.getMenuButtonBoundingClientRect()
					customBar = custom.bottom + custom.top - e.statusBarHeight
					navbar = (custom.top - e.statusBarHeight) * 2 + custom.height
					// #endif
					// #ifdef MP-ALIPAY
					statusBar = e.statusBarHeight
					customBar = e.statusBarHeight + e.titleBarHeight
					// #endif
					// #ifdef APP-PLUS
					console.log('app-plus', e)
					statusBar = e.statusBarHeight
					customBar = e.statusBarHeight + 45
					// #endif
					// #ifdef H5
					statusBar = 0
					customBar = e.statusBarHeight + 45
					// #endif
					this.StatusBarRpx = statusBar;
				}
			})
			this.initTime();
			this.addFlags();
			this.disableScreenRecording();
			this.watchTime = 0;
		},
		onUnload() {
			this.editWeek();
			this.clearTime();
		},
		onReady() {

		},
		onShow() {
			
		},
		onHide() {
			this.clearFlags();
		},
		onResize() {

		},
		onPullDownRefresh() {

		},
		onReachBottom(e) {

		},
		onPageScroll(e) {

		},
		methods: {
			initTime() {
				if (this.timer) {
					this.timer = null;
					this.watchTime = 0;
				}
				this.timer = setInterval(() => {
					this.watchTime += 1;
				}, 1000)
			},
			clearTime() {
				if (this.timer) {
					clearInterval(this.timer);
					this.timer = null;
				}
			},
			editWeek() {
				this.$api.post(global.apiUrls.post65dd94c3edd23, {
					chapter_id: this.id,
					learning_duration: this.watchTime
				}).then(res => {
					console.log(res);
					if (res.data.code == 1) {
						// console.log('请求成功');
					} else {
						// console.log('请求失败');
					}
				})
			},
			// 禁止录屏
			disableScreenRecording() {
				const disableScreenRecordingScript = `
			        (function() {
			          // 这段代码在webview页面的上下文中执行
			          window.addEventListener('contextmenu', function(e) {
			            e.preventDefault();
			          });
			          window.addEventListener('keydown', function(e) {
			            // 阻止"Print Screen"键被按下
			            if (e.key === 'Print' || e.key === 's' && e.ctrlKey && e.shiftKey) {
			              e.preventDefault();
			            }
			          });
			        })();
			      `;
				// 注入禁止录屏的脚本
				uni.postMessage({
					data: disableScreenRecordingScript
				});
			},
			addFlags() {
				// #ifdef APP-PLUS
				let osname = plus.os.name
				if (osname == "Android") {
					// var activity = plus.android.runtimeMainActivity()
					// plus.android.invoke(plus.android.invoke(activity, "getWindow"), "addFlags", 0x00002000)
					// this.recordObj = uni.requireNativePlugin("me-record");
					// this.recordObj.noRecord();
				} else {
					// 监听录屏功能
					let self = this;
					self.benMusic = uni.requireNativePlugin('BenBen-music');
					self.benMusic.addScreenshots(() => {
						setTimeout(() => {
							uni.navigateBack();
							self.benMusic.removeScreenshots({});
						}, 300);
					});
				}
				// #endif
			},
			//  允许截屏
			clearFlags() {
				// #ifdef APP-PLUS
				let osname = plus.os.name
				if (osname == "Android") {
					// var activity = plus.android.runtimeMainActivity()
					// plus.android.invoke(plus.android.invoke(activity, "getWindow"), "clearFlags", 0x00002000)
					// this.recordObj.allowRecord();
					// this.recordObj = "";
				} else {
					this.benMusic.removeScreenshots({});
				}
				// #endif
			},
		}
	};
</script>
<style lang="scss" scoped>
	.ben_fu-jyf-parser_2 {
		background-color: #fff;
		padding-left: 32rpx;
		padding-right: 32rpx;
		padding-top: 0rpx;
		padding-bottom: 0rpx;

	}

	.purchaseData_fd1_0 {
		font-size: 32rpx;
		font-weight: 500;
		color: #333;
	}

	.purchaseData_flex_1 {
		padding: 24rpx 32rpx 32rpx 32rpx;
	}

	.purchaseData_fd0_0 {
		margin: 0rpx 32rpx 0rpx 32rpx;
	}

	.purchaseData_fd0_0_c0 {
		width: 200rpx;
	}

	.purchaseData_fd0_0_c0_c0 {
		width: 20rpx;
		height: 36rpx;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
	}

	.purchaseData_fd0_0_c1_c0 {
		font-size: 36rpx;
		font-weight: 500;
		color: #333;
		line-height: 88rpx;
		-webkit-line-clamp: 1;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
	}

	.purchaseData_fd0_0_c2 {
		width: 200rpx;
	}

	.purchaseData_flex_0 {
		border-bottom: 1px solid #eee;
		background: #fff;
		background-size: 100% !important;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		top: 0rpx;
		background-size: 100% auto !important;
	}

	.page {
		min-height: calc(100vh - var(--window-bottom));
		background: #fff;
		background-size: 100% auto !important;
	}
</style>